package com.Xht.www.servlet;

//后端响应查看签到历史的请求

import com.Xht.www.dao.UserCheckinDAO;
import com.Xht.www.po.UserCheckinPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/getCheckinHistory")
public class CheckinHistoryServlet extends HttpServlet {
    private UserCheckinDAO userCheckinDAO = new UserCheckinDAO();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        List<UserCheckinPO> checkinHistory = userCheckinDAO.getCheckinHistoryByUserId(userId);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        StringBuilder json = new StringBuilder("[");
        for (int i = 0; i < checkinHistory.size(); i++) {
            UserCheckinPO checkin = checkinHistory.get(i);
            json.append("{\"checkinTime\":\"").append(checkin.getCheckinTime().toString()).append("\"");
            json.append(",\"pointsEarned\":").append(checkin.getPointsEarned()).append("}");
            if (i < checkinHistory.size() - 1) {
                json.append(",");
            }
        }
        json.append("]");
        out.print(json.toString());
        out.flush();
    }
}